#include <stdio.h>

struct matrix {
    int data[10][10];
    int size;
};
struct matrix matrix;

void init_matrix() {
    int cnt = 1;
    for (int y = 0; y < matrix.size; y++) {
        for (int x = 0; x < matrix.size; x++) {
            matrix.data[y][x] = cnt++;
        }
    }
}

void swap_array(int* array, int length, int cnt, int dir) {
    int temp[10];
    for (int i = 0; i < length; i++) {
        if (dir) {
            int pos = i - (cnt % length);
            if (pos < 0) {
                pos += length;
            }
            temp[i] = array[pos];
        } else {
            temp[i] = array[(i + cnt) % length];
        }
    }
    for (int i = 0; i < length; i++) {
        array[i] = temp[i];
    }
}

void print_matrix() {
    for (int y = 0; y < matrix.size; y++) {
        for (int x = 0; x < matrix.size; x++) {
            printf("%d", matrix.data[y][x]);
            if (y == matrix.size - 1 && x == matrix.size - 1) {
                putchar('\n');
            } else {
                putchar(' ');
            }
        }
    }
}

int main(void) {
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
        int n, m;
        scanf("%d%d", &n, &m);
        matrix.size = n;
        init_matrix();
        for (int j = 0; j < m; j++) {
            char c;
            int a, b;
            scanf(" %c%d%d", &c, &a, &b);
            int data[10];
            switch (c) {
                case 'L':
                    swap_array(matrix.data[a - 1], matrix.size, b, 0);
                    break;
                case 'R':
                    swap_array(matrix.data[a - 1], matrix.size, b, 1);
                    break;
                case 'U':
                    for (int i = 0; i < matrix.size; i++) {
                        data[i] = matrix.data[i][a - 1];
                    }
                    swap_array(data, matrix.size, b, 0);
                    for (int i = 0; i < matrix.size; i++) {
                        matrix.data[i][a - 1] = data[i];
                    }
                    break;
                case 'D':
                    for (int i = 0; i < matrix.size; i++) {
                        data[i] = matrix.data[i][a - 1];
                    }
                    swap_array(data, matrix.size, b, 1);
                    for (int i = 0; i < matrix.size; i++) {
                        matrix.data[i][a - 1] = data[i];
                    }
                    break;
            }
        }
        print_matrix();
    }
}
